﻿using PMSWenLvTing.DAL;
using PMSWenLvTing.Model.TongJi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PMSWenLvTing.BLL
{
    /// <summary>
    /// 专门做统计的BLL类
    /// </summary>
    public class TongJiBLL
    {
        TongJiDAL dal = new TongJiDAL();

        /// <summary>
        /// 获取月报的相关统计数据
        /// </summary>
        /// <returns></returns>
        public DTO_YueBao_TJ Get_YueBao_TJ(string strWhere)
        {
            return dal.Get_YueBao_TJ(strWhere);
        }

        /// <summary>
        /// 根据条件 获取数据中心页面的 工作督导统计数据
        /// </summary>
        /// <param name="lisjdid">酒店id</param>
        /// <param name="dts">开始时间</param>
        /// <param name="dte">结束时间</param>
        /// <param name="bbtype">报表类型 1月报 2年报</param>
        /// <returns></returns>
        public DTO_BBZT_TJ Get_BBZhuangTai_TJ(List<int> lisjdid, DateTime? dts, DateTime? dte, int bbtype)
        {
            #region 初始化一个结果数据
            DTO_BBZT_TJ modelRes = new DTO_BBZT_TJ();
            modelRes.n_daishenhe = 0;
            modelRes.n_yichehui = 0;
            modelRes.n_yishenhe = 0;
            modelRes.n_yitijiao = 0;
            modelRes.sh_wanchenglv = "0%";
            modelRes.tijiaolv = "0%";
            #endregion

            #region 方法变量，有效性判断
            if (lisjdid == null || lisjdid.Count <= 0)
            {
                return modelRes;
            }
            string jdid_str = string.Join(",", lisjdid);
            string sql = "";
            string strDT = "";
            if (bbtype == 1)
            {
                strDT += " and leixing=1";
                if (dts != null)
                {
                    strDT += string.Format(" and bb_riqi>='{0}' ", dts.Value.ToString("yyyy-MM") + "-01");
                }
                if (dte != null)
                {
                    strDT += string.Format(" and bb_riqi<='{0}' ", dte.Value.ToString("yyyy-MM") + "-01");
                }
            }
            else if (bbtype == 2)
            {
                strDT += " and leixing=2";
                if (dts != null)
                {
                    strDT += string.Format(" and bb_riqi>='{0}' ", dts.Value.ToString("yyyy") + "-01-01");
                }
                if (dte != null)
                {
                    strDT += string.Format(" and bb_riqi<='{0}' ", dte.Value.ToString("yyyy") + "-01-01");
                }
            }

            #endregion

            #region 分析数据
            sql = string.Format("select Count(*) from baobiao_zhuangtai where jdid in ({0}) and zt_shenhe=0 and isdel=false", jdid_str) +strDT;
            modelRes.n_daishenhe = dal.Get_Int_BySql(sql);
            sql = string.Format("select Count(*) from baobiao_zhuangtai where jdid in ({0}) and zt_shenhe=2 and isdel=false", jdid_str) + strDT;
            modelRes.n_yichehui = dal.Get_Int_BySql(sql);
            sql = string.Format("select Count(*) from baobiao_zhuangtai where jdid in ({0}) and zt_shenhe=1 and isdel=false", jdid_str) + strDT;
            modelRes.n_yishenhe = dal.Get_Int_BySql(sql);
            sql = string.Format("select Count(*) from baobiao_zhuangtai where jdid in ({0}) and zt_tianbao=1 and isdel=false", jdid_str) + strDT;
            modelRes.n_yitijiao = dal.Get_Int_BySql(sql);
            int sumSH = modelRes.n_daishenhe + modelRes.n_yichehui + modelRes.n_yishenhe;
            modelRes.sh_wanchenglv = sumSH == 0 ? "0%" : (modelRes.n_yishenhe * 1.00 / sumSH).ToString("P");
            modelRes.tijiaolv = sumSH == 0 ? "0%" : (modelRes.n_yitijiao * 1.00 / sumSH).ToString("P");
            #endregion

            return modelRes;

        }




    }
}